package framework.aop.aspect;

import framework.aop.intercept.InchMethodInterceptor;
import framework.aop.intercept.InchMethodInvocation;

import java.lang.reflect.Method;

/**
 * class for the methods that should run after the point cut throws an Exception
 */
public class InchMethodAfterThrowingAdvice extends InchAbstractAspectAdvice implements InchAdvice, InchMethodInterceptor {

    /**
     * constructor
     * @param aspectMethod aspect method eg: demo.aspect.logAspect.before
     * @param aspectTarget aspect class eg: demo.aspect.logAspect
     */
    public InchMethodAfterThrowingAdvice(Method aspectMethod, Object aspectTarget) {
        super(aspectMethod, aspectTarget);
    }

    /**
     * invoke method that run after there is an exception threw by point cut class
     * @param methodInvocation  join point
     * @return  the result if possible
     * @throws Throwable the Exception throws by the point cut method
     */
    @Override
    public Object invoke(InchMethodInvocation methodInvocation) throws Throwable {
        //try to run and catch exception
        try {
            return methodInvocation.proceed();
        } catch (Throwable throwable) {
            invokeAdviceMethod(methodInvocation,null,throwable.getCause());
            throw throwable;
        }
    }
}
